package data;

import java.util.ArrayList;
import java.util.Iterator;

import data.model.Action;

/**
 * Container for Actions, 
 * 
 * @author Will Home
 *
 */
public class ActionList {

	private static ArrayList<Action> actList;
	
	public ActionList(){
		actList = new ArrayList<Action>();
		actList.add(new Action("Undefined"));
	}
	
	/**
	 * check whether an action is in the list, if so return it.
	 * 
	 * @param name action name
	 * @return action
	 */
	public static Action getAction(String name)
	{
		Action curr;
		Iterator<Action> it = actList.iterator();
		
		while(it.hasNext())
		{
			curr = it.next();
			if(curr.getActName().equals(name))
				return curr;
		}
		
		return null;
	}
	
	/**
	 * 
	 * @return Array of current Actions
	 */
	public static Object[] getList()
	{
		return actList.toArray();
	}
	
	/**
	 * check whether an action is in the list, if so return it.
	 * 
	 * @param name action ID
	 * @return action
	 */
	public static Action getAction(int actID)
	{
		Action curr;
		Iterator<Action> it = actList.iterator();
		
		while(it.hasNext())
		{
			curr = it.next();
			if(curr.getActID() == actID)
				return curr;
		}
		
		return null;
	}
	
	/**
	 * adds a new action to the list
	 * 
	 * @param name - action name
	 * @return success check
	 */
	public static boolean addAction(String name)
	{
		return actList.add(new Action(name));
	}
	
	/**
	 * removes the given action from the list
	 * 
	 * @param actID
	 * @return success check
	 */
	public static boolean removeAction(int actID)
	{
		Action curr;
		if(actID != 0)
		{
			Iterator<Action> it = actList.iterator();

			while(it.hasNext())
			{
				curr = it.next();
				if(curr.getActID() == actID)
				{
					it.remove();
					return true;
				}
			}
		}
		return false;
	}
	
	/**
	 * removes the given action from the list
	 * 
	 * @param name
	 * @return success check
	 */
	public static boolean removeAction(String name)
	{
		if(!name.equals("No action"))
		{
			Iterator<Action> it = actList.iterator();
			while(it.hasNext())
			{
				if(it.next().getActName().equals(name))
				{
					it.remove();
					return true;
				}
			}
		}
		return false;
	}

	/**
	 * 
	 * @return number of actions in list
	 */
	public static int size()
	{
		return actList.size();
	}
	
	/**
	 * remove all actions
	 */
	public static void clear()
	{
		actList.clear();
		actList.add(new Action("Undefined"));
	}
	
	/**
	 * Changes the action associated with a given task
	 * 
	 * @param actID
	 * @param taskID
	 * @param newActID
	 * @return success check
	 */
	public static boolean changeAction(int actID, int taskID, int newActID)
	{
		boolean check = false;
		//Removes task from current action
		check = getAction(actID).removeTask(taskID);
		
		//if successful, add task to new action
		if(check)
			check = getAction(newActID).addTask(taskID);
		
		return check;
	}
}
